package com.gqz.equip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.entity.EnWzJfglEntity;
import com.gqz.equip.mapper.EnWzJfglMapper;
import com.gqz.equip.mapstruct.EnWzJfglMapstruct;
import com.gqz.equip.service.IEnWzJfglService;
import com.gqz.equip.vo.EnWzJfglVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 机房信息表 ҵ���߼�ʵ�ֲ�ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class EnWzJfglServiceImpl extends ServiceImpl<EnWzJfglMapper, EnWzJfglEntity> implements IEnWzJfglService {

    @Autowired
    private EnWzJfglMapstruct mapstruct;

    @Override
    public List<EnWzJfglVo> getById(String id) {
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = new MPJLambdaWrapper<>();
        /** todo getPsrId-> todo-must�޸�Ϊ��¼�������ֶ�todo */
        queryWrapper.eq(EnWzJfglEntity::getId, id);
        queryWrapper.selectAll(EnWzJfglEntity.class);
        List<EnWzJfglEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnWzJfglVo> page(RequestForm<EnWzJfglVo> requestForm) {
        EnWzJfglEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnWzJfglEntity.class);
        queryWrapper.orderByDesc(EnWzJfglEntity::getCreateTime);
        Page<EnWzJfglEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnWzJfglEntity> iPage = baseMapper.selectJoinPage(page, EnWzJfglEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnWzJfglVo> requestForm) {
        EnWzJfglEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfglEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must�޸�Ϊ��¼�������ֶ�todo */
        //queryWrapper.select(EnWzJfglEntity::getPsrId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnWzJfglVo> forms) {
        List<EnWzJfglEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfglEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnWzJfglVo> forms) {
        List<EnWzJfglEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfglEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    public int deleteByid(List<String> ids) {
        int result = 0;
        for (int i = 0, size = ids.size(); i < size; i++) {
            String id = ids.get(i);
            result += baseMapper.deleteById(id);
        }
        return 0;
    }


}
